// ============================================================================
//
// Copyright (C) 2006-2008 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design;

import static org.eclipse.draw2d.PositionConstants.BOTTOM;
import static org.eclipse.draw2d.PositionConstants.CENTER;
import static org.eclipse.draw2d.PositionConstants.LEFT;
import static org.eclipse.draw2d.PositionConstants.MIDDLE;
import static org.eclipse.draw2d.PositionConstants.RIGHT;
import static org.eclipse.draw2d.PositionConstants.TOP;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_BOTTOM;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_CENTER;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_LEFT;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_MIDDLE;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_RIGHT;
import static org.eclipse.gef.ui.actions.GEFActionConstants.ALIGN_TOP;
import static org.eclipse.gef.ui.actions.GEFActionConstants.MATCH_HEIGHT;
import static org.eclipse.gef.ui.actions.GEFActionConstants.MATCH_WIDTH;

import org.dengues.designer.ui.editors.MultiPageEditorContributor;
import org.dengues.reports.editor.forms.ReportDesignEditor;
import org.eclipse.gef.ui.actions.AlignmentRetargetAction;
import org.eclipse.gef.ui.actions.MatchHeightRetargetAction;
import org.eclipse.gef.ui.actions.MatchWidthRetargetAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-8-21 qiang.zhang $
 * 
 */
public class RepDesignBarContributor extends MultiPageEditorContributor {

    public static int[] ALIG_ACTION_IDS = new int[] { BOTTOM, CENTER, LEFT, MIDDLE, RIGHT, TOP };

    public static String[] ALIG_ACTION_IDSTR = new String[] { ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_LEFT, ALIGN_MIDDLE, ALIGN_RIGHT,
            ALIGN_TOP };

    /**
     * Qiang.Zhang.Adolf@gmail.com RepDesignBarContributor constructor comment.
     */
    public RepDesignBarContributor() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToToolBar(org.eclipse.jface.action.IToolBarManager)
     */
    @Override
    public void contributeToToolBar(IToolBarManager toolBarManager) {
        super.contributeToToolBar(toolBarManager);
        for (String id : ALIG_ACTION_IDSTR) {
            toolBarManager.add(getAction(id));
        }
        toolBarManager.add(getAction(MATCH_WIDTH));
        toolBarManager.add(getAction(MATCH_HEIGHT));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.editors.MultiPageEditorContributor#init(org.eclipse.ui.IActionBars)
     */
    @Override
    public void init(IActionBars bars) {
        for (int i = 0; i < ALIG_ACTION_IDS.length; i++) {
            addDesignRetargetAction(new AlignmentRetargetAction(ALIG_ACTION_IDS[i]));
        }
        addDesignRetargetAction(new MatchWidthRetargetAction());
        addDesignRetargetAction(new MatchHeightRetargetAction());
        super.init(bars);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.editors.MultiPageEditorContributor#getAction(org.eclipse.ui.IEditorPart,
     * java.lang.String)
     */
    @Override
    protected IAction getAction(IEditorPart editor, String actionID) {
        if (editor instanceof ReportDesignEditor) {
            return ((ReportDesignEditor) editor).getAction(actionID);
        }
        return super.getAction(editor, actionID);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.dengues.designer.ui.editors.MultiPageEditorContributor#setGlobalActionHandlers(org.eclipse.ui.IEditorPart,
     * org.eclipse.ui.IActionBars)
     */
    @Override
    protected void setGlobalActionHandlers(IEditorPart activeEditor, IActionBars actionBars) {
        super.setGlobalActionHandlers(activeEditor, actionBars);
        for (String id : ALIG_ACTION_IDSTR) {
            actionBars.setGlobalActionHandler(id, getAction(activeEditor, id));
        }
        actionBars.setGlobalActionHandler(MATCH_WIDTH, getAction(activeEditor, MATCH_WIDTH));
        actionBars.setGlobalActionHandler(MATCH_HEIGHT, getAction(activeEditor, MATCH_HEIGHT));
    }
}
